package com.hmeter.mongodb;

import java.net.UnknownHostException;
import java.util.Map;

import com.mongodb.BasicDBObject;
import com.mongodb.util.JSON;
import com.mongodb.util.JSONParseException;

public class MongoDbCreateSamplerUtility {

	private MongoDbUtility mongoDbUtility = null;
	
	public MongoDbCreateSamplerUtility(String host, int port, String dbName, String collectionName) throws UnknownHostException
	{
		mongoDbUtility = new MongoDbUtility(host, port, dbName, collectionName);
	}
	
	public MongoDbUtility getMongoDbUtility()
	{
		return mongoDbUtility;
	}
	
	public String writeDocumentIntoCollection(Map<String, Object> data)
	{
		BasicDBObject doc = mongoDbUtility.makeSimpleDocument(data);
		mongoDbUtility.getMongoDbCollection().insert(doc);
		return "Write completed";
	}
	
	public String writeJSONContentIntoCollection(String jsonContent)
	{
		try {
			BasicDBObject doc = (BasicDBObject)JSON.parse(jsonContent);
			mongoDbUtility.getMongoDbCollection().insert(doc);
		} catch (JSONParseException ex) {
			System.out.println("Cannot parse invalid json content");
		}
		
		return "Write completed";
	}
}
